// index.js
import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex)

export default new Vuex.Store({
	state: {
		user:{
			nick_name:"未登录",
			bg_color:"666",
			head_ico:"https://baigongbao.oss-cn-beijing.aliyuncs.com/img/head.png",
			status:"1"
		},
		tags:[],
		is_login:0,
		like:"",
		upload_info:[],
		msg_count:0,
		editing:0

	},
	getters: {
		msg_count(state){
			return state.msg_count;
		},
		upload_info(state){
			// console("hello");
			// for (var i = state.upload_info.length - 1; i >= 0; i--) {

			//}

			return state.upload_info;
		},
		like(state){
			return state.like;
		},
		tags(state){
			return state.user;
		},
		language(state){
			return state.language;
		},
		user(state){
			if (localStorage.token != "") {
				return state.user;
			}else{
				state.user = {
					nick_name:"未登录",
					bg_color:"666",
					head_ico:"https://baigongbao.oss-cn-beijing.aliyuncs.com/img/head.png",
					status:"1"
				}
				return state.user;
			}

		},
		editing(state){
			return state.editing;
		},
		is_login(state){
			var that = this;
			if (localStorage.token != "") {
				state.is_login = 1;
			}else{
				state.is_login = 0;
				localStorage.chat_list=[];
			}
			return state.is_login;
		}
	},
	mutations: {

		set_upload_info(state,ui){

			var has = 0;
			for (var i = state.upload_info.length - 1; i >= 0; i--) {



				if (state.upload_info[i].name == ui.name) {
					has = 1;
					state.upload_info[i].percent = ui.percent;

					if (ui.percent == 100) {

						state.upload_info.splice(i,1);
						return;
					}
				}
			}

			if (has == 0 && ui.percent != 100) {
				state.upload_info.push(ui);
			}



		},
		set_like(state,like){
			state.like = like
		},
		set_edit(state,edit){
			state.editing = edit
		},
		get_msg_count(state){
			const params = new URLSearchParams();
			params.append("token",localStorage.token);

			axios.post('https://service.baigongbao.com/Api/get_msg_count',params).then((res) => {
				state.msg_count = res.data.data;
            }).catch((err) => {
				console.log(err)
            })
		},
		get_user_tags(state){

		},
		get_user_info(state){
			const params = new URLSearchParams();
			params.append("token",localStorage.token);

			axios.post('https://service.baigongbao.com/Api/get_user_info',params).then((res) => {
				res.data.data.is_main = localStorage.is_main
				state.user = res.data.data;
            }).catch((err) => {
				console.log(err)
            })


		},
		fresh_is_login(state) {
			var that = this;
			if (localStorage.token != "") {
				state.is_login = 1;
				return state.is_login;
			}else{
				state.is_login = 0;
				state.user = {
					nick_name:"未登录",
					bg_color:"666",
					head_ico:"https://baigongbao.oss-cn-beijing.aliyuncs.com/img/head.png"
				}
				return state.is_login;
			}

	    }
	},
	actions: {
		set_lang(context){
			context.commit('set_lang');
		},
		set_upload_info(context){
			context.commit('set_upload_info')
		},
		set_like(context){
			context.commit('set_like')
		},
		get_user_tags(context){
			context.commit('get_user_tags')
		},
		get_msg_count(context){
			context.commit('get_msg_count')
		},
		get_user_info(context){
			context.commit('get_user_info')
		},
		fresh_is_login(context){
			context.commit('fresh_is_login')
		}
	}
})
